package com.situ.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.context.BaseContext;
import com.situ.domain.dto.ProductDTO;
import com.situ.domain.entity.TProduct;
import com.situ.domain.dto.SearchPageQueryDTO;
import com.situ.domain.vo.ProductQueryVO;
import com.situ.mapper.TProductMapper;
import com.situ.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<TProductMapper, TProduct> implements ProductService {


    private final TProductMapper productMapper;

    /**
     * 分页搜索商品
     * @param pageQueryDTO
     * @return
     */
    @Override
    public ProductQueryVO searchProductsByPage(SearchPageQueryDTO pageQueryDTO) {
        Page<TProduct> page = Page.of(pageQueryDTO.getCurrentPage(),
                pageQueryDTO.getPageSize());
        Page<TProduct> page2 = productMapper
                .searchProductsByPage(page, pageQueryDTO.getSearchKey());
        ProductQueryVO productQueryVO = new ProductQueryVO();
        productQueryVO.setPageList(page2.getRecords());
        productQueryVO.setCurrentPage(page2.getCurrent());
        productQueryVO.setPageSize(page2.getSize());
        productQueryVO.setPageTotal(page2.getTotal());
        productQueryVO.setPages(page2.getPages());
        return productQueryVO;
    }


    /**
     * 添加商品
     * @param productDTO
     */
    @Override
    public void addProduct(ProductDTO productDTO) {
        TProduct product = new TProduct();
        BeanUtil.copyProperties(productDTO, product);
        product.setCreatedTime(new Date());
        product.setCreatedUser(BaseContext.getCurrentId().toString());
        this.save(product);
    }
}
